package com.gexiaobao.pigeon.viewmodel

import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.viewModelScope
import com.gexiaobao.pigeon.app.model.bean.ApplyPayDepositResponse
import com.gexiaobao.pigeon.app.model.bean.AuctionAddressResponse
import com.gexiaobao.pigeon.app.model.bean.AuctionDescResponse
import com.gexiaobao.pigeon.app.model.bean.AuctionGoodsDetailResponse
import com.gexiaobao.pigeon.app.model.bean.AuctionHomeResponse
import com.gexiaobao.pigeon.app.model.bean.AuctionOrderListResponse
import com.gexiaobao.pigeon.app.model.bean.BiddingHistoryResponse
import com.gexiaobao.pigeon.app.model.bean.FollowAuctionResponse
import com.gexiaobao.pigeon.app.model.bean.FollowGoodsListBean
import com.gexiaobao.pigeon.app.model.bean.FreezeMoneyResponse
import com.gexiaobao.pigeon.app.model.bean.PayOrderInfoResponse
import com.gexiaobao.pigeon.app.model.bean.ProxyAppDetailResponse
import com.gexiaobao.pigeon.app.model.bean.RuleDetailContentResponse
import com.gexiaobao.pigeon.app.model.bean.ThematicListBean
import com.gexiaobao.pigeon.app.model.bean.ThematicListResponse
import com.gexiaobao.pigeon.app.model.bean.UiState
import com.gexiaobao.pigeon.app.model.param.ReSendSMSParam
import com.gexiaobao.pigeon.app.network.ListDataUiState
import com.gexiaobao.pigeon.app.network.apiService
import com.gexiaobao.pigeon.app.util.RxToast
import com.google.gson.Gson
import kotlinx.coroutines.launch
import me.hgj.jetpackmvvm.base.viewmodel.BaseViewModel
import me.hgj.jetpackmvvm.ext.request
import me.hgj.jetpackmvvm.util.LogUtils
import okhttp3.MediaType.Companion.toMediaTypeOrNull
import okhttp3.RequestBody
import okhttp3.RequestBody.Companion.toRequestBody

/**
 * @Author      : hxw
 * @Date        : 2024/11/7 9:07
 * @Describe    :
 */
class AuctionViewModel : BaseViewModel() {

    private var pageSize: Int = 20
    private var pageNo: Int = 1

    /** 获取支付信息 */
    val payInfoResult: MutableLiveData<PayOrderInfoResponse> = MutableLiveData()
//    fun getPayOrderInfo(body: RequestBody) {
//        request({ apiService.getPayOrderInfo() }, {
//            payInfoResult.value = it
//        }, {
//            RxToast.error(it.msg)
//        })
//    }

//    /** 申请支付 */
//    val applyOrderResult: MutableLiveData<ApplyPayDepositResponse> = MutableLiveData()
//    fun applyAuctionOrder(body: RequestBody) {
//        request({ apiService.applyAuctionOrder(body) }, {
//            applyOrderResult.value = it
//        }, {
//            RxToast.error(it.msg)
//        })
//    }

    /** 获取支付信息并申请支付 */
    val applyOrderResult: MutableLiveData<ApplyPayDepositResponse> = MutableLiveData()
    fun getPayInfoAndApplyPay(isUse: Boolean, mOrderId: Int) {
        viewModelScope.launch {
            try {
                val result1 = apiService.getPayOrderInfo(mOrderId, isUse)
                payInfoResult.value = result1.data
                val orderTotalMoney = result1.data.orderTotalMoney

                val map = mapOf("isUseDeposit" to isUse, "orderId" to mOrderId, "totalMoney" to orderTotalMoney)
                val body: RequestBody = Gson().toJson(map).toRequestBody("application/json".toMediaTypeOrNull())

                request({ apiService.applyAuctionOrder(body) }, {
                    applyOrderResult.value = it
                }, {
                    RxToast.error(it.msg)
                })
            } catch (e: Exception) {
                LogUtils.warnInfo("MainViewModel", "Error occurred: ${e.localizedMessage}")
            }
        }
    }

    /** 设置用户拍卖收货地址 */
    val setAuctionAddressResult: MutableLiveData<Any> = MutableLiveData()
    fun setAuctionAddress(body: RequestBody) {
        request({ apiService.setAuctionAddress(body) }, {
            setAuctionAddressResult.value = it
        }, {
            RxToast.error(it.msg)
        })
    }

    /** 确认收货 */
    val receiveOrderResult: MutableLiveData<Any> = MutableLiveData()
    fun receiveAuctionOrder(body: RequestBody) {
        request({ apiService.receiveAuctionOrder(body) }, {
            receiveOrderResult.value = it
        }, {
            RxToast.error(it.msg)
        })
    }

    /** 获取用户拍卖收货地址 */
    val auctionAddressResult: MutableLiveData<AuctionAddressResponse> = MutableLiveData()
    fun getAuctionAddress() {
        request({ apiService.getAuctionAddress() }, {
            auctionAddressResult.value = it
        }, {
            RxToast.error(it.msg)
        })
    }

    /** 创建用户配置 */
    fun createAuctionUserConf() {
        request({ apiService.createAuctionUserConf() }, {
        }, {
        })
    }

    /** 编辑代理 */
    val editProxyAppResult: MutableLiveData<Any> = MutableLiveData()
    fun editProxyApp(body: RequestBody) {
        request({ apiService.editProxyApp(body) }, {
            editProxyAppResult.value = it
        }, {
            RxToast.error(it.msg)
        })
    }

    /** 取消代理 */
    val delProxyAppResult: MutableLiveData<Any> = MutableLiveData()
    fun delProxyApp(proxyId: String) {
        request({ apiService.delProxyApp(proxyId) }, {
            delProxyAppResult.value = it
        }, {
            RxToast.error(it.msg)
        })
    }

    /** 获取代理详情 */
    val getProxyAppResult: MutableLiveData<ProxyAppDetailResponse> = MutableLiveData()
    fun getProxyApp(proxyId: String) {
        request({ apiService.getProxyApp(proxyId) }, {
            getProxyAppResult.value = it
        }, {
            RxToast.error(it.msg)
        })
    }

    /** 确认支付 */
    val payAuctionResult: MutableLiveData<UiState> = MutableLiveData()
    fun payAuctionOrder(body: RequestBody) {
        request({ apiService.payAuctionOrder(body) }, {
            val result = UiState(isSuccess = true)
            payAuctionResult.value = result
        }, {
            val result = UiState(isSuccess = false, errorMsg = it.msg)
            payAuctionResult.value = result
        })
    }

    /** 退还保证金 */
    val returnMarginResult: MutableLiveData<Any> = MutableLiveData()
    fun returnBond(body: RequestBody) {
        request({ apiService.returnBond(body) }, {
            returnMarginResult.value = it
        }, {
            RxToast.error(it.msg)
        })
    }

    /** 获取app拍卖会详情描述 */
    val auctionDescResult: MutableLiveData<AuctionDescResponse> = MutableLiveData()
    fun getAuctionDescApp(auctionId: String) {
        request({ apiService.getAuctionDescApp(auctionId) }, {
            auctionDescResult.value = it
        }, {
            RxToast.error(it.msg)
        })
    }

    /** 重新发送验证码  */
    var reSendSMSResult: MutableLiveData<UiState> = MutableLiveData()
    fun reSendSMS(body: ReSendSMSParam) {
        request(
            { apiService.reSendSMS(body) }, {
                val result = UiState(isSuccess = true)
                reSendSMSResult.value = result
            }, {
                val result = UiState(isSuccess = false, errorMsg = it.msg)
                reSendSMSResult.value = result
            }
        )
    }

    /** 获取拍卖会的关注或者参与 */
    val followAuctionsResult: MutableLiveData<ListDataUiState<FollowAuctionResponse>> = MutableLiveData()
    fun getFollowAndJoinAuctionList(isRefresh: Boolean, followType: Int) {
        if (isRefresh) {
            pageNo = 1
        }
        request({ apiService.getFollowAndJoinAuctionList(pageSize, pageNo, followType) }, {
            pageNo++
            val listDataUiState = ListDataUiState(
                isSuccess = true,
                isRefresh = isRefresh,
                isEmpty = it.isEmpty(),
                total = it.total,
                hasMore = it.hasMore(),
                isFirstEmpty = isRefresh && it.isEmpty(),
                listData = it.list
            )
            followAuctionsResult.value = listDataUiState
        }, {
            val listDataUiState = ListDataUiState(
                isSuccess = false,
                isRefresh = isRefresh,
                errMessage = it.msg,
                listData = arrayListOf<FollowAuctionResponse>()
            )
            followAuctionsResult.value = listDataUiState
        })
    }

    /** 关注商品或者拍卖会 */
    val followGoodsResult: MutableLiveData<ListDataUiState<FollowGoodsListBean>> = MutableLiveData()
    fun getFollowAndJoinGoodList(isRefresh: Boolean, followType: Int) {
        if (isRefresh) {
            pageNo = 1
        }
        request({ apiService.getFollowAndJoinGoodList(pageSize, pageNo, followType) }, {
            pageNo++
            val listDataUiState = ListDataUiState(
                isSuccess = true,
                isRefresh = isRefresh,
                isEmpty = it.isEmpty(),
                total = it.total,
                hasMore = it.hasMore(),
                isFirstEmpty = isRefresh && it.isEmpty(),
                listData = it.list
            )
            followGoodsResult.value = listDataUiState
        }, {
            val listDataUiState = ListDataUiState(
                isSuccess = false,
                isRefresh = isRefresh,
                errMessage = it.msg,
                listData = arrayListOf<FollowGoodsListBean>()
            )
            followGoodsResult.value = listDataUiState
        })
    }

    /** 获取加价规则详情 */
    val ruleDetailResult: MutableLiveData<RuleDetailContentResponse> = MutableLiveData()
    fun getAddRuleDetailApp(auctionId: String) {
        request({ apiService.getAddRuleDetailApp(auctionId) }, {
            ruleDetailResult.value = it
        }, {
            RxToast.error(it.msg)
        })
    }

    /** 获取保证金信息 */
    val freezeMoneyResult: MutableLiveData<FreezeMoneyResponse> = MutableLiveData()
    fun getTotalFreezeMoney() {
        request({ apiService.getTotalFreezeMoney() }, {
            freezeMoneyResult.value = it
        }, {
            RxToast.error(it.msg)
        })
    }

    /** 关注商品或者拍卖会 */
    val followOperationResult: MutableLiveData<Any> = MutableLiveData()
    fun followOperation(body: RequestBody) {
        request({ apiService.followOperation(body) }, {
            followOperationResult.value = it
        }, {
            RxToast.error(it.msg)
        })
    }

    /** 申请缴纳保证金 */
    val applyPayDepositResult: MutableLiveData<ApplyPayDepositResponse> = MutableLiveData()
    fun applyPayDeposit(body: RequestBody) {
        request({ apiService.applyPayDeposit(body) }, {
            applyPayDepositResult.value = it
        }, {
            RxToast.error(it.msg)
        })
    }

    /** 确认缴纳保证金 */
    val confirmPayDepositResult: MutableLiveData<UiState> = MutableLiveData()
    fun confirmPayDeposit(body: RequestBody) {
        request({ apiService.confirmPayDeposit(body) }, {
            val result = UiState(isSuccess = true)
            confirmPayDepositResult.value = result
        }, {
            val result = UiState(isSuccess = false, errorMsg = it.msg)
            confirmPayDepositResult.value = result
        })
    }

    /** 获取app专题列表 */
    val appAuctionListResultTitle: MutableLiveData<ThematicListResponse> = MutableLiveData()
    fun getAppAuctionListTitle(auctionId: String) {
        request({ apiService.getAppAuctionListTitle(auctionId, pageSize, pageNo) }, {
            appAuctionListResultTitle.value = it
        }, {
            RxToast.error(it.msg)
        })
    }

    /** 获取竞价历史 */
    val appPriceHistoryResult: MutableLiveData<ListDataUiState<BiddingHistoryResponse>> = MutableLiveData()
    fun getAppPriceHistory(isRefresh: Boolean, goodId: String) {
        if (isRefresh) {
            pageNo = 1
        }
        request({ apiService.getAddPriceHistory(goodId, pageNo, pageSize) }, {
            pageNo++
            val listDataUiState = ListDataUiState(
                isSuccess = true,
                isRefresh = isRefresh,
                isEmpty = it.isEmpty(),
                total = it.total,
                hasMore = it.hasMore(),
                isFirstEmpty = isRefresh && it.isEmpty(),
                listData = it.list
            )
            appPriceHistoryResult.value = listDataUiState
        }, {
            val listDataUiState = ListDataUiState(
                isSuccess = false,
                isRefresh = isRefresh,
                errMessage = it.msg,
                listData = arrayListOf<BiddingHistoryResponse>()
            )
            appPriceHistoryResult.value = listDataUiState
        })
    }

    /** 获取app订单列表 */
    val auctionOrderListResult: MutableLiveData<ListDataUiState<AuctionOrderListResponse>> = MutableLiveData()
    fun getAuctionOrderListApp(isRefresh: Boolean, orderStatus: String) {
        if (isRefresh) {
            pageNo = 1
        }
        request({ apiService.getAuctionOrderListApp(pageSize, pageNo, orderStatus) }, {
            pageNo++
            val listDataUiState = ListDataUiState(
                isSuccess = true,
                isRefresh = isRefresh,
                isEmpty = it.isEmpty(),
                total = it.total,
                hasMore = it.hasMore(),
                isFirstEmpty = isRefresh && it.isEmpty(),
                listData = it.list
            )
            auctionOrderListResult.value = listDataUiState
        }, {
            val listDataUiState = ListDataUiState(
                isSuccess = false,
                isRefresh = isRefresh,
                errMessage = it.msg,
                listData = arrayListOf<AuctionOrderListResponse>()
            )
            auctionOrderListResult.value = listDataUiState
        })
    }

    /** 获取app专题列表 */
    val appAuctionListResult: MutableLiveData<ListDataUiState<ThematicListBean>> = MutableLiveData()
    fun getAppAuctionList(isRefresh: Boolean, auctionId: String) {
        if (isRefresh) {
            pageNo = 1
        }
        request({ apiService.getAppAuctionList(auctionId, pageSize, pageNo) }, {
            pageNo++
            val listDataUiState = ListDataUiState(
                isSuccess = true,
                isRefresh = isRefresh,
                isEmpty = it.isEmpty(),
                total = it.total,
                hasMore = it.hasMore(),
                isFirstEmpty = isRefresh && it.isEmpty(),
                listData = it.list
            )
            appAuctionListResult.value = listDataUiState
        }, {
            val listDataUiState = ListDataUiState(
                isSuccess = false,
                isRefresh = isRefresh,
                errMessage = it.msg,
                listData = arrayListOf<ThematicListBean>()
            )
            appAuctionListResult.value = listDataUiState
        })
    }

    /** 代理出价 */
    val proxyPriceResult: MutableLiveData<UiState> = MutableLiveData()
    fun proxyPrice(body: RequestBody) {
        request({ apiService.proxyPrice(body) }, {
            val success = UiState(isSuccess = true)
            proxyPriceResult.value = success
        }, {
            val err = UiState(isSuccess = false, errorMsg = it.msg, errorCode = it.code)
            proxyPriceResult.value = err
        })
    }

    /** 立即出价 */
    val offerPriceResult: MutableLiveData<UiState> = MutableLiveData()
    fun offerPrice(body: RequestBody) {
        request({ apiService.offerPrice(body) }, {
            val success = UiState(isSuccess = true)
            offerPriceResult.value = success
        }, {
            val err = UiState(isSuccess = false, errorMsg = it.msg, errorCode = it.code)
            offerPriceResult.value = err
        })
    }

    /** 获取app拍卖商品详情 */
    val auctionGoodDetailResult: MutableLiveData<AuctionGoodsDetailResponse> = MutableLiveData()
    fun getAuctionGoodDetail(goodId: String) {
        request({ apiService.getAuctionGoodDetail(goodId) }, {
            auctionGoodDetailResult.value = it
        }, {
            RxToast.error(it.msg)
        })
    }

    /** 获取app拍卖首页 */
    val auctionHomeResult: MutableLiveData<AuctionHomeResponse> = MutableLiveData()
    fun getAuctionHome() {
        request({ apiService.getAuctionHome() }, {
            auctionHomeResult.value = it
        }, {
            RxToast.error(it.msg)
        })
    }

}